package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-25 14:52
 */
public class QLint928 {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }

        int[] res = new int[256];
        int i = 0;//左指针
        int max = 0;
        int c = 0;//不同字符的数量
        for (int j = 0; j < s.length(); j++) {
            res[s.charAt(j)]++;
            if(res[s.charAt(j)] == 1){
                c++;
            }

            while(c > 2) {
                res[s.charAt(i)]--;
                if(res[s.charAt(i)] == 0) {
                    c--;
                }
                i++;
            }
            max = Math.max(max, j - i + 1);
        }
        return max;
    }
}
